﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using DTO;
using System.Data.SqlClient;

namespace Persistence
{
	public class CourseController
	{
        public static void insertCourse(Course vCo)
        { 
            //Se crea un comando.
            MySqlCommand comm = DBConnection.getInstance().createCommand();
            //Se le asigna el tipo.
            comm.CommandType = CommandType.StoredProcedure;
            //Se le asigna el nombre del procedimiento almacenado.
            comm.CommandText = "proin_Course";

            //Se asignan los parametros al comando.
            comm.Parameters.AddWithValue("@pconame", truncate(vCo.StrName.ToString(), 40));
            comm.Parameters.AddWithValue("@pcoteacherid", Convert.ToInt16(vCo.TchTeacher.LngId));
            comm.Parameters.AddWithValue("@pcoplace", truncate(vCo.StrPlace.ToString(), 50));
            comm.Parameters.AddWithValue("@pcoprice", vCo.FltPrice);
            comm.Parameters.AddWithValue("@pcodescripcion", truncate(vCo.StrDescription.ToString(), 200));

            //Se ejecuta el comando.
            DBConnection.getInstance().executeNonQueryCommand(comm);
        }

        //Se obtiene un curso por su Id.
        public static Course getCourseById(long vId)
        {
            string sql = "SELECT * FROM courses WHERE coid = @id;";
            MySqlCommand comm = DBConnection.getInstance().createCommand();
            comm.CommandText = sql;
            comm.Parameters.AddWithValue("id", Convert.ToInt16(vId));

            //Se devuelve una instancia de persona
            return parseRegToCourse(DBConnection.getInstance().executeSelectCommand(comm));
        }

        //Se actualizan los datos de un curso.
        public static void updateCourse(Course c)
        {
            Course cOriginal = getCourseById(c.LngId);

            if (!c.StrName.Equals(cOriginal.StrName))
            {
                Helper.updateProperty("courses", "coname",c.StrName.ToString(), "coid", cOriginal.LngId, 40);
            }
            if (!c.TchTeacher.LngId.Equals(cOriginal.TchTeacher.LngId))
            {
                Helper.updateProperty("courses", "coteacherid", c.TchTeacher.LngId, "coid", cOriginal.LngId);
            }
            if (!c.StrPlace.Equals(cOriginal.StrPlace))
            {
                Helper.updateProperty("courses", "coplace", c.StrName.ToString(), "coid", cOriginal.LngId, 50);
            }
            if (!c.FltPrice.Equals(cOriginal.FltPrice))
            {
                Helper.updateProperty("courses", "coprice", c.FltPrice, "coid", cOriginal.LngId);
            }
            if (!c.StrDescription.Equals(cOriginal.StrDescription))
            {
                Helper.updateProperty("courses", "codescription", c.StrDescription.ToString(), "coid", cOriginal.LngId, 200);
            }
        }

        //Recupera un registro correspondiente a un curso en la base de datos, y asigna sus datos a una instancia de Course.
        public static Course parseRegToCourse(DataTable pTable)
        {
            Course aCourse = new Course();

            aCourse.LngId = long.Parse(pTable.Rows[0]["coid"].ToString());
            aCourse.StrName = pTable.Rows[0]["coname"].ToString();
            aCourse.TchTeacher = TeacherController.getTeacherById(long.Parse(pTable.Rows[0]["coteacherid"].ToString()));
            aCourse.StrPlace = pTable.Rows[0]["coplace"].ToString();
            aCourse.FltPrice = float.Parse(pTable.Rows[0]["coprice"].ToString());
            aCourse.StrDescription = pTable.Rows[0]["codescription"].ToString();

            return aCourse;
        }


        private static string truncate(string unSt, int maxLength)
        {
            return unSt.Length <= maxLength ? unSt : unSt.Substring(0, maxLength);
        }
	}
}
